﻿PongGame = Eg.Game.extend({
  gravity: 0,
  init: function () {
    this.loadLevel(PongLevel1);
  },
  loadLevel: function (level) {
    this.parent(level);
    this.player1 = this.getEntitiesByType(EntityPlayer)[0];
    this.player2 = this.getEntitiesByType(EntityPlayer)[1];
    this.ball = this.getEntitiesByType(EntityBall)[0];
  },
  update: function () {
    if (Eg.input.state('left')) {
      this.player1.accel.x = -this.player1.accelDef.air;
    } else if (Eg.input.state('right')) {
      this.player1.accel.x = this.player1.accelDef.air;
    } else {
      this.player1.accel.x = 0;
    }

    this.parent();
  }
});

EntityPlayer = Eg.Entity.extend({
  collides: Eg.Entity.COLLIDES.FIXED,
  size: {
    x: 48,
    y: 8
  },
  accelDef: {
    air: 200
  },
  animSheet: new Eg.AnimationSheet('player.png', 48, 8),
  init: function (x, y, settings) {
    this.parent(x, y, settings);
    this.addAnim('idle', 1, [0]);
  },
  update: function () {
    this.parent();
    this.setAnimation();
  },

  setAnimation: function () {
    this.currentAnim = this.anims.idle;
  },
  collideWith: function (other, axis) {
    if (axis == 'y' && this.standing && this.currentAnim != this.anims.die) {
      this.currentAnim.update();
      this.setAnimation();
    }
  }
});

EntityBall = Eg.Entity.extend({
  collides: Eg.Entity.COLLIDES.PASSIVE,
  bounciness: 1,
  size: {
    x: 8, y: 8
  },
  animSheet: new Eg.AnimationSheet('ball.png', 8, 8),
  init: function (x, y, settings) {
    this.parent(x, y, settings);
    this.addAnim('idle2', 1, [0]);

    this.vel.x = this.maxVel.x * 0.5;
    this.vel.y = this.maxVel.y * 0.5;
    //this.vel.y = 2 * this.maxVel.y * Math.random() - this.maxVel.y;
  },
  update: function () {
    this.parent();
    this.currentAnim = this.anims.idle2;
  },
  check: function (other) {
    var g = other;
    this.vel.x = -this.vel.x;
  },
  collideWith: function (other, axis) {
    
  }
});